periodicjs.core.protocols
Description
Core protocols is a component of periodicjs.core.controller that exposes a standard set of methods across different transport protcols with options for implementing different API strategies. With core protocols implementing a RESTful API for HTTP requests looks no different than standing up a JSONRPC API for websocket based communications.
Usage (basic)
const express = require('express');
const mongoose = require('mongoose');
const ProtocolInterface = require('periodicjs.core.protocols');
const DBInterface = require('periodicjs.core.data');
const ResponderInterface = require('periodicjs.core.responder');
mongoose.connect();
mongoose.connection.once('open', () => {
let Example = mongoose.model('Example');
let Server = express().listen(3000);
let http_adapter = ProtocolInterface.create({
express: Server,
resources: {},
db: {
example: DBInterface.create({ adapter: 'mongo', model: Example });
},
responder: ResponderInterface.create({ adapter: 'json' }),
api: 'rest',
adapter: 'http'
});
http_adapter.implement();
});
Usage (advanced)
let http_adapter = ProtocolInterface.create({
express: require('express'),
resources: {},
db: {
example: DBInterface.create({ adapter: 'mongo', model: Example })
},
responder: ResponderInterface.create({ adapter: 'json' }),
api: 'rest',
adapter: 'http'
});
let router = http_adapter.api.implement({ model_name: 'example', dirname: ['./some/path/to/view/dir'] });
Server.use('/api/v1', router);
adapter.implement vs. adapter.api.implement
let http_adapter = ProtocolInterface.create({
express: Server,
resources: {},
db: {
example: DBInterface.create({ adapter: 'mongo', model: Example })
},
responder: ResponderInterface.create({ adapter: 'json' }),
api: 'rest',
adapter: 'http'
});
http_adapter.implement();
let router = http_adapter.api.implement({ model_name: 'example' });
console.log(router);
Development
Make sure you have grunt installed
$ npm install -g grunt-cli jsdoc-to-markdown
For generating documentation
$ grunt doc
$ jsdoc2md adapters/**/*.js api_adapters/**/*.js utility/**/*.js index.js > doc/api.md
Notes
Testing
$ npm i
$ grunt test
Contributing
License
MIT